<script setup lang="ts">
import {ref, render} from 'vue'
//引入VueOfficePdf组件
import VueOfficePdf from '@vue-office/pdf'
const isShow = ref(1);
const docxText = ref('http://static.shanhuxueyuan.com/test.pdf');
const rendered = () => {
  console.log('渲染完成了')
}

const handleShow = (id:any) => {
  switch (id) {
    case 1:
      isShow.value = 1;
      docxText.value = 'http://static.shanhuxueyuan.com/test.pdf'
      break;
    case 2:
      isShow.value = 2;
      docx.value = ''
    default:
      break;
  }
}
const docx = ref<any>('')
// 点击上传
const file = ref();
const fileClick = () => {
  file.value.click()
}
const fileChange = (event: any) => {
  const files = event.target.files[0];
  const reader = new FileReader();
  reader.readAsArrayBuffer(files);
  reader.onload = () => {
    docx.value = reader.result;
  }
}
</script>

<template>
  <div>
    <h1 style="font-size: 18px; text-align: center;">pdf文档预览</h1>
    <ul>
      <li :class="isShow == 1?'redBorder': ''" @click="handleShow(1)">网络地址</li>
      <li :class="isShow == 2?'redBorder': ''" @click="handleShow(2)">本地上传</li>
    </ul>
    <div class="boxItem">
      <input v-if="isShow==1" type="text" v-model="docxText" disabled="true">
      <div class="btn" v-else>
        <button @click="fileClick">立即上传</button>
        <input type="file" ref="file" @change="fileChange" style="display: none;">
      </div>
    </div>
  </div>

  <div class="preview" v-if="isShow==1">
    <vue-office-pdf style="min-height: 500px;" :src="docxText" @rendered="rendered"/>
  </div>
  <div class="preview" v-if="isShow==2 && docx">
    <vue-office-pdf style="min-height: 500px;" :src="docx"/>
  </div>
</template>